import { fileURLToPath, URL } from 'node:url'
import createVitePlugins from './vite/plugins'
import { defineConfig, loadEnv } from 'vite'

// https://vitejs.dev/config/
export default defineConfig(( mode, command )=> {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_ENV } = env

  return {
    base: './',
    plugins: createVitePlugins(env, command === 'build'),
    resolve: {
      alias: {
        '~': fileURLToPath(new URL('./', import.meta.url)),
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    server: {
      host: '0.0.0.0' ,
      proxy: {
        '/console/fire-digitaltwin-screen': {
          target: 'https://bimdta-dev.bwton-console.cn',
          changeOrigin: true,
        }
      }
    },

    build: {
      // 设置输出目录
      outDir: 'dist',
      // 设置静态资源目录
      assetsDir: 'static',
      sourcemap: true,
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          }
        }
      }
    }
  }
})
